<?php

class loginform extends formEngine {

    protected $formName = 'loginform';
    protected $resultAction = 'closeFormWindow';
    protected $resultPage = 'homeLogin';

    public function load($page) {

        $cssClass = 'loginFormIframe';

        $this->addComponent('textfield', array('label' => 'Gebruikersnaam', 'value' => '', 'name' => 'username', 'cssClass' => $cssClass));
        $this->addComponent('password', array('label' => 'Wachtwoord', 'value' => '', 'name' => 'password', 'cssClass' => $cssClass));
        $this->addComponent('button', array('id' => 'testButton', 'value' => 'Login', 'name' => 'login', 'cssClass' => 'loginButton'));

        parent::load($page);
    }

    public function validate(&$values) {

        $error .= "";

        if($values['username'] == '') {
            $error .= "Geen inlognaam ingevoerd \n";
        }

        if($values['password'] == '') {
            $error .= "Geen wachtwoord ingevoerd \n";
        }

        if($error != ""){
            return $error;
        }

        $query = 'SELECT * FROM klanten WHERE gebruikersnaam = "'.$values['username'].'" and wachtwoord = md5("'.$values['password'].'")';
        $result = mysql_query($query);

        if(mysql_num_rows($result) > 0) {

						$line = mysql_fetch_assoc($result);
						$values['klantid'] = $line['ID'];

            return true;
        } else {
            return "Inloggen mislukt";
        }

	return true;

    }

    public function save(&$values) {

        // We zijn succesvol ingelogd
        $_SESSION['authenticated'] = true;
				$_SESSION['klantid'] = $values['klantid'];

        return true;

    }

}

?>
